package com.common.baselibrary.utils;

/**
 * The type Number util.
 *
 * @author MaTianyu
 * @date 2014 -11-21
 */
public class NumberUtil {

    /**
     * Convert toint int.
     *
     * @param intStr   the int str
     * @param defValue the def value
     * @return the int
     */
    public static int convertToInt(String intStr, int defValue) {
        try {
            return Integer.parseInt(intStr);
        } catch (Exception e) {
        }
        return defValue;
    }

    /**
     * Convert tolong long.
     *
     * @param longStr  the long str
     * @param defValue the def value
     * @return the long
     */
    public static long convertTolong(String longStr, long defValue) {
        try {
            return Long.parseLong(longStr);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return defValue;
    }

    /**
     * Convert tofloat float.
     *„
     * @param fStr     the f str
     * @param defValue the def value
     * @return the float
     */
    public static float convertTofloat(String fStr, float defValue) {
        try {
            return Float.parseFloat(fStr);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return defValue;
    }

    /**
     * Convert todouble double.
     *
     * @param dStr     the d str
     * @param defValue the def value
     * @return the double
     */
    public static double convertTodouble(String dStr, double defValue) {
        try {
            return Double.parseDouble(dStr);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return defValue;
    }


    /**
     * Convert to string string.
     *
     * @param o        the o
     * @param defValue the def value
     * @return the string
     */
    public static String convertToString(Object o,String defValue){
        try{
            return String.valueOf(o);
        }catch(Exception ex){
            //e.printStackTrace();
        }
        return defValue;
    }

    public static String intToIp(int i) {
        return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + (i >> 24 & 0xFF);
    }
}
